package com.dianmi.seckill.common.log.impl;

import com.dianmi.seckill.common.log.LogCutter;
import com.dianmi.seckill.common.log.LogFactory;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * @author xieqiaofu
 * @date 2020/11/18
 */
public class DefaultLogCutter implements LogCutter {

    private LogContext logContext;

    private Method method;

    private LogFactory logFactory;

    public DefaultLogCutter(MethodSignature signature) {
        this.logContext = LogContext.getInstance();
        this.method = signature.getMethod();
        this.logFactory = new DefaultLogFacotry(method, logContext);
    }

    @Override
    public void beforeMethod(Object[] args) {
        this.logContext.register(this);
        this.logContext.log(logFactory.create(args));
    }

    @Override
    public void afterMethod(Object returnObj) {
        this.logContext.log(logFactory.create(returnObj));
        this.logContext.complete(this);
    }

    @Override
    public void throwMethod(Throwable e) {
        this.logContext.log(logFactory.create(e));
        this.logContext.complete(this);
    }
}
